
#include "student.h"
#include<istream>
#include<string>
#include<iostream>
#include "Primarystudent.h"
using namespace std;

void PrimaryStudent::input() {
    cout << "请输入学号: ";
    (cin>> studentId);
    cout << "请输入姓名: ";
    cin >>(name); 
    cout << "请输入性别(M/F): ";
    cin >> gender;
    cout << "请输入年龄: ";
    cin >> age;
    cin.ignore();
    cout << "请输入班级: ";
    (cin>>className);
    
    cout << "请输入英语成绩: ";
    cin >> english;
    cout << "请输入数学成绩: ";
    cin >> math;
    cout << "请输入语文成绩: ";
    cin >> chinese;
    cin.ignore();
}

void PrimaryStudent::display() const {
    cout << "学号: " << studentId << endl;
    cout << "姓名: " << name << endl;
    cout << "性别: " << gender << endl;
    cout << "年龄: " << age << endl;
    cout << "班级: " << className << endl;
    cout << "英语成绩: " << english << endl;
    cout << "数学成绩: " << math << endl;
    cout << "语文成绩: " << chinese << endl;
    cout << "总分: " << calculateTotalScore() << endl;
}

double PrimaryStudent::calculateTotalScore() const {
    return english + math + chinese;
}

void PrimaryStudent::save(ofstream& out) const {
    // 写入学生类型
    string type = "PrimaryStudent";
    out.write(type.c_str(), type.size() + 1);

    // 写入基本信息
    out.write(studentId.c_str(), studentId.size() + 1);
    out.write(name.c_str(), name.size() + 1);
    out.write(&gender, sizeof(gender));
    out.write(reinterpret_cast<const char*>(&age), sizeof(age));
    out.write(className.c_str(), className.size() + 1);

    // 写入成绩
    out.write(reinterpret_cast<const char*>(&english), sizeof(english));
    out.write(reinterpret_cast<const char*>(&math), sizeof(math));
    out.write(reinterpret_cast<const char*>(&chinese), sizeof(chinese));
}

void PrimaryStudent::load(ifstream& in) {
    char buffer[256];
    
    // 读取基本信息
    //in.getline(buffer, 256, '\0');
    //studentId = buffer;
    in.getline(buffer, 256, '\0');
    studentId = buffer;
    
    in.getline(buffer, 256, '\0');
    name = buffer;
    
    in.read(&gender, sizeof(gender));
    in.read(reinterpret_cast<char*>(&age), sizeof(age));
    
    in.getline(buffer, 256, '\0');
    className = buffer;
    
    // 读取成绩
    in.read(reinterpret_cast<char*>(&english), sizeof(english));
    in.read(reinterpret_cast<char*>(&math), sizeof(math));
    in.read(reinterpret_cast<char*>(&chinese), sizeof(chinese));
}

